package grape.attend.service.user.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import grape.attend.service.user.api.IUserLeaveService;
import grape.attend.service.user.po.UserLeave;
import grape.attend.service.user.po.UserLeaveApplication;
import grape.attend.service.user.mapper.UserLeaveApplicationMapper;
import grape.attend.service.user.api.IUserLeaveApplicationService;
import grape.base.rest.client.dict.DictClient;
import grape.base.service.dict.po.Dict;
import grape.common.service.common.BaseServiceImpl;
import grape.workflow.rest.client.process.ProcessClient;
import org.flowable.rest.service.api.runtime.process.ProcessInstanceCreateRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 用户请/补假单表 服务实现类
 * </p>
 *
 * @author yangwei
 * @since 2020-05-29
 */
@Service
public class UserLeaveApplicationServiceImpl extends BaseServiceImpl<UserLeaveApplicationMapper, UserLeaveApplication> implements IUserLeaveApplicationService {

    @Autowired
    private DictClient dictClient;
    @Autowired
    private IUserLeaveService iUserLeaveService;

    @Autowired
    private ProcessClient processClient;
    @Override
    public UserLeaveApplication addUserLeaveApplication(UserLeaveApplication userLeaveApplication, List<UserLeave> userLeaves,Boolean isSubmitApprove) {
        UserLeaveApplication.DictApproveStatus approveStatus = null;
        if (isSubmitApprove) {
            userLeaveApplication.setApproveSubmitAt(System.currentTimeMillis());
            approveStatus = UserLeaveApplication.DictApproveStatus.user_leave_application_approve_inapprove;
        }else {
            approveStatus = UserLeaveApplication.DictApproveStatus.user_leave_application_approve_unsubmit;

        }
        Dict dict = dictClient.getDictByCode(approveStatus.name());
        userLeaveApplication.setApproveStatusDictId(dict.getId());
        userLeaveApplication.setSerialNo(IdWorker.getIdStr());
        userLeaveApplication = insert(userLeaveApplication);
        batchInsertUserLeaves(userLeaveApplication, userLeaves);
        if(isSubmitApprove){
            ProcessInstanceCreateRequest processInstanceCreateRequest = new ProcessInstanceCreateRequest();
            processInstanceCreateRequest.setBusinessKey(userLeaveApplication.getId());
            processInstanceCreateRequest.setProcessDefinitionKey(UserLeaveApplication.PROCESS_DEFINITION_KEY);
            processClient.createProcessInstance(processInstanceCreateRequest);
        }
        return userLeaveApplication;
    }

    private boolean batchInsertUserLeaves(UserLeaveApplication userLeaveApplication, List<UserLeave> userLeaves){
        for (UserLeave userLeave : userLeaves) {
            userLeave.setUserLeaveApplicationId(userLeaveApplication.getId());
            userLeave.setClasseDateStart(DateUtil.date(userLeave.getStartAt()));
            userLeave.setClasseDateEnd(DateUtil.date(userLeave.getEndAt()));
        }
        return iUserLeaveService.saveBatch(userLeaves);
    }
    @CacheEvict(value="getById",key = "#userLeaveApplication.id")
    @Override
    public UserLeaveApplication updateUserLeaveApplication(UserLeaveApplication userLeaveApplication, List<UserLeave> userLeaves, Boolean isSubmitApprove) {
        // 如果在审批中，不能变更 请在调用该方法时确认
        UserLeaveApplication.DictApproveStatus approveStatus = null;
        if (isSubmitApprove) {
            userLeaveApplication.setApproveSubmitAt(System.currentTimeMillis());
            approveStatus = UserLeaveApplication.DictApproveStatus.user_leave_application_approve_inapprove;
        }else {
            approveStatus = UserLeaveApplication.DictApproveStatus.user_leave_application_approve_unsubmit;

        }
        Dict dict = dictClient.getDictByCode(approveStatus.name());
        userLeaveApplication.setApproveStatusDictId(dict.getId());
        boolean r = updateById(userLeaveApplication);
        if (!r) {
            return null;
        }
        // 先删除再添加
        iUserLeaveService.removeByUserLeaveApplicationId(userLeaveApplication.getId());
        batchInsertUserLeaves(userLeaveApplication, userLeaves);
        if(isSubmitApprove){
            ProcessInstanceCreateRequest processInstanceCreateRequest = new ProcessInstanceCreateRequest();
            processInstanceCreateRequest.setBusinessKey(userLeaveApplication.getId());
            processInstanceCreateRequest.setProcessDefinitionKey(UserLeaveApplication.PROCESS_DEFINITION_KEY);
            processClient.createProcessInstance(processInstanceCreateRequest);
        }
        return userLeaveApplication;
    }
    @CacheEvict(value="getById",key = "#id")
    @Override
    public boolean removeById(Serializable id) {
        boolean r = super.removeById(id);
        if (r) {
            iUserLeaveService.removeByUserLeaveApplicationId((String) id);
        }
        return r;
    }
}
